package javaLanguage.base;

public class singleTon {
    private singleTon() {}

    private static class singleTonHolder{
        private static singleTon instanceOb = new singleTon();
    }

    public static singleTon getInstance(){
        return singleTonHolder.instanceOb;
    }
}


class singleTon2 {
    private singleTon2(){}

    private static volatile singleTon2 ins;
    //加volatile是为了防止指令重排，从而返回未被初始化的实例

    public static singleTon2 getInstance(){
        if(ins == null){
            //当实例为null时，才进行创建
            synchronized (singleTon2.class){
                // 二次检查的意义在于如果多个线程同时通过了第一次的检查，防止反复创建；
                if(ins == null) {
                    ins = new singleTon2();
                }
            }
        }

        return ins;
    }
}

enum singleTon3{
    INSTANCE;
    public void printf(){
        System.out.println(INSTANCE.getClass());
    }
}
